package com.lzy.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.common.util.EnumUtil;
import com.lzy.product.constants.BusinessType.ProfitErrorStatus;
import com.lzy.product.dao.ProfitErrorMapperExt;
import com.lzy.product.dtos.profit.ProfitErrorDto;
import com.lzy.product.entity.ProfitError;
import com.lzy.product.service.ProfitErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分利错误记录服务
 * Created by WIN7 on 2017/12/5.
 */
@Service
public class ProfitErrorServiceImpl implements ProfitErrorService {

    @Autowired
    private ProfitErrorMapperExt profitErrorMapperExt;

    @Override
    public Long insert(ProfitErrorDto dto) {
        ProfitError profitError = DozerUtils.map(dto, ProfitError.class);
        return profitErrorMapperExt.insertSelective(profitError) > 0 ? profitError.getOrderId() : null;
    }

    @Override
    public Boolean update(ProfitErrorDto dto) {
        return profitErrorMapperExt.updateByPrimaryKeySelective(DozerUtils.map(dto, ProfitError.class)) > 0;
    }

    @Override
    public Boolean delete(Long id) {
        return profitErrorMapperExt.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Boolean remove(Long id) {
        return null;
    }

    @Override
    public ProfitErrorDto findById(Long id) {
        return DozerUtils.map(profitErrorMapperExt.selectByPrimaryKey(id), ProfitErrorDto.class);
    }

    @Override
    public List<ProfitErrorDto> query(ProfitErrorDto dto) {
        return profitErrorMapperExt.selectList(dto);
    }

    @Override
    public PageInfo<ProfitErrorDto> selectList(Pager pager, ProfitErrorDto paramDto) {
        if (pager != null) {
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        }
        List<ProfitErrorDto> list = profitErrorMapperExt.selectList(paramDto);
        if (list != null) {
            for (ProfitErrorDto dto : list) {
                if(EnumUtil.valueOf(ProfitErrorStatus.class, dto.getStatus()) != null) {
                    dto.setStatusStr(EnumUtil.valueOf(ProfitErrorStatus.class, dto.getStatus()).getDesc());
                }
            }
        }
        return new PageInfo<>(list);
    }
}
